package com.example.administrator.questionanswer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.questionanswer.application.MyApplication;
import com.example.administrator.questionanswer.dao.DaoSession;
import com.example.administrator.questionanswer.dao.UserBeanDao;
import com.example.administrator.questionanswer.greendao.UserBean;
import com.example.administrator.questionanswer.utils.Const;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/4/2 0002.
 */

public class LoginActivity extends Activity {
    @BindView(R.id.edit_user_name)
    EditText editUserName;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.txt_register)
    TextView txtRegister;
    @BindView(R.id.btn_login)
    Button btnLogin;
    DaoSession daoSession;
    UserBeanDao userBeanDao;

    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        daoSession = MyApplication.getInstances().getDaoSession();
        sharedPreferences = getSharedPreferences(Const.USER_INFO, MODE_PRIVATE);
    }

    public static final int REGISTER = 1;

    @OnClick({R.id.txt_register, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_register:
                startActivityForResult(new Intent(this, RegisterActivity.class), REGISTER);
                break;
            case R.id.btn_login:
                String userName = editUserName.getText().toString();
                String password = editPassword.getText().toString();
                if (userName == null || "".equals(userName)) {
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password == null || "".equals(password)) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                login(userName, password);


                break;
        }
    }

    private void login(String userName, String password) {
        userBeanDao = daoSession.getUserBeanDao();
        List<UserBean> userBeans = userBeanDao.loadAll();
        if (userBeans.size() > 0) {
            for (UserBean userBean : userBeans) {
                boolean hasUser = userName.equals(userBean.getName());
                if (hasUser) {
                    boolean isRight = password.equals(userBean.getPassword());
                    if (isRight) {
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putString(Const.USER_NAME, userName);
                        edit.putBoolean(Const.USER_ONLINE, true);
                        edit.commit();
                        startActivity(new Intent(this, FunctionSelectActivity.class));
                        return;
                    } else {
                        Toast.makeText(this, "密码错误，请重新输入", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } else {
                    continue;
                }
            }
            showDialog();
        } else {
            Toast.makeText(this, "您还未注册，请先注册", Toast.LENGTH_SHORT).show();
            showDialog();
            return;
        }
    }


    AlertDialog alertDialog;

    public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        alertDialog = builder.create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        WindowManager windowManager = window.getWindowManager();
        Display defaultDisplay = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = (int) (defaultDisplay.getWidth() * 0.8);
        window.setAttributes(attributes);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_register, null);
        alertDialog.setContentView(view);
        window.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_white));
        TextView txtsure = (TextView) view.findViewById(R.id.txt_accept);
        TextView txtcancle = (TextView) view.findViewById(R.id.txt_refuse);
        txtsure.setOnClickListener(onClickListener);
        txtcancle.setOnClickListener(onClickListener);
        alertDialog.setCanceledOnTouchOutside(false);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            switch (view.getId()) {
                case R.id.txt_accept:
                    alertDialog.dismiss();
                    startActivityForResult(new Intent(LoginActivity.this, RegisterActivity.class), REGISTER);
                    break;
                case R.id.txt_refuse:
                    alertDialog.dismiss();
                    break;
            }
        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REGISTER) {
                String name = data.getStringExtra("name");
                String password = data.getStringExtra("password");
                login(name, password);
            }
        }
    }
}
